package com.zyb.insurance.common;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.*;

public class ElasticsearchClient {

    private RestHighLevelClient restHighLevelClient;

    private ElasticsearchClient() {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "Ruyuan2020"));

        RestClientBuilder builder = RestClient.builder(new HttpHost("192.168.21.177", 9200))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                });

        this.restHighLevelClient = new RestHighLevelClient(builder);
    }

    public RestHighLevelClient getRestHighLevelClient() {
        return restHighLevelClient;
    }

    private static class Singleton {

        static ElasticsearchClient instance = new ElasticsearchClient();

    }

    public static ElasticsearchClient getInstance() {
        return Singleton.instance;
    }

}
